/**
 * Wedding
 *
 * @author Manohar Viswanathan
 */
class Wedding { 

	FamilyMember mainMember
	FamilyMember spouseMember
	Date weddingDate
	Boolean active = true
	
    static hasMany = [children:FamilyMember]
    static mappedBy = [children:"parent"]
	
    static mapping = {
      table 'ft_wedding'
       version false
    }
    
	static constraints = {
		weddingDate(blank:true, nullable:true, validator:{ val, obj -> 
		   def memMain = obj.properties['mainMember']
		   def memSpouse = obj.properties['spouseMember']
		   if(memMain.birthDate?.after(val)) return ["not.before.birthDate", memMain.nickname] 
		   if(memMain.deathDate?.before(val)) return ["not.after.deathDate", memMain.nickname]
		   if(memSpouse.birthDate?.after(val)) return ["not.before.birthDate", memSpouse.nickname]
		   if(memSpouse.deathDate?.before(val)) return ["not.after.deathDate", memSpouse.nickname]
		})
	}
	
	String toString() {
	   if (id==0) return "Family Root"
	   return "${mainMember?.nickname} and ${spouseMember?.nickname}"
	}
	
    def getChildrenSorted() {
      def sortedChildren = new ArrayList()
	  sortedChildren.addAll(children)
	  sortedChildren.sort { it.birthDate }
	  
	  return sortedChildren 
    }
    
    
}